<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="x-ua-compatible" content="ie=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="author" content="chanlong"/>
    <meta name="keywords" content="蜜娃娃, 早教, 缤够, 云服务"/>
    <meta name="description" content="蜜娃娃云服务，专业提供早教领域，企业及机构，运营管理类，互联网产品"/>
    <link rel="stylesheet" href="/miwawa/resources/lib/bootstrap/3.3.5/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="/miwawa/resources/fonts/font-awesome/4.5.0/css/font-awesome.css" />
    <title>jpush</title>
</head>

<body>
    <div class="container">
        <div>
           <form>
           		<div class="form-group">
                   <div>
                       <label>通知标题</label>
                   </div>
                   <div>
                       <input id="msgTitle" type="text" maxlength="20" placeholder="可输入20字" class="form-control" autofocus />
                   </div>
               </div>
               <div class="form-group">
                   <div>
                       <label>推送内容</label>
                   </div>
                   <div>
                       <textarea id="msgContent" rows="3" maxlength="58" placeholder="可输入58字" class="form-control"></textarea>
                   </div>
               </div>

               <div class="form-group">
                   <div>
                       <label>机构选择</label>
                   </div>
                   <div class="form-inline">
                       <select id="centerSelect" class="form-control" onchange="getShops(this)">
							
                       </select>
                       <select id="shopSelect" class="form-control">
							<option value="all">请选择分支机构</option>
                       </select>
                   </div>
                   
               </div>
<!--                <div class="form-group"> -->
<!--                    <div> -->
<!--                        <select id="shopSelect" class="form-control left"> -->
<!-- 							<option value="all">请选择分支机构</option> -->
<!--                        </select> -->
<!--                    </div> -->
<!--                </div> -->
               <div class="form-group">
                   <div>
                       <label>推送平台</label>
                   </div>
                   <div>
                       <ul class="list-inline">
                           <li>
                               <label>
                                   <input id="id_aps" type="checkbox" checked="checked">
                                  <span>
                                        <i class="fa fa-apple"></i>
                                        <span> iOS</span>
                                    </span>
                               </label>
                           </li>
                           <li>
                               <label>
                                   <input id="id_android" type="checkbox" checked="checked">
                                  <span>
                                        <i class="fa fa-android" aria-hidden="true"></i>
                                        <span> Android</span>
                                    </span>
                               </label>
                           </li>
                       </ul>
                    </div>
                </div>
               <button type="button" class="btn btn-primary right" onclick="sendJpushMessage()">发送</button>
           </form>
        </div>
    </div>
<script src="/miwawa/resources/lib/miwawa/1.0.2/js/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    $(function () {
        getCenters();
    });

   function getCenters() {
        $.ajax({
            url : "/miwawa/default/app/search.json",
            type : "POST",
            dataType : "json",
            data : {
                "entityClass" : "com.app.miwawa.api.system.entity.Center",
                "and[0][operator]": "EQ",
                "and[0][fieldName]": "invalid",					// 删除标识
                "and[0][fieldValue]": "F",						// F=有效的<未删除>、T=无效的<已删除>
                "and[1][operator]": "EQ",
                "and[1][fieldName]": "status",					// 状态标识
                "and[1][fieldValue]": "0"						// 默认为0
            },
            async : false,
            error : function(data) {
                alert("error" + data.status);
            },
            success : function(data) {
                console.info(data);

                if(data.success == true){
                    var typeTitle = "请选择早教中心";

                    var inner='<option value="all">'+typeTitle+'</option>';
                    var dataList = data.data.rows;
                    if (data != null) {
                        for(var i=0;i<dataList.length;i++){

                            inner+='<option value="'+dataList[i].dbname +'">'+dataList[i].name+'</option>';
                        }
                    }
                    $("#centerSelect").html(inner);
                }
            }
        });
    }

    // 获取机构列表
    function getShops(obj) {
        $.ajax({
            url : "/miwawa/default/app/search.json",
            type : "POST",
            dataType : "json",
            data : {
                "entityClass" : "com.app.miwawa.api.app.entity.view.ShopView",
                "and[0][operator]": "EQ",
                "and[0][fieldName]": "invalid",					// 删除标识
                "and[0][fieldValue]": "F",						// F=有效的<未删除>、T=无效的<已删除>
                "and[1][operator]": "EQ",
                "and[1][fieldName]": "status",					// 状态标识
                "and[1][fieldValue]": "0",						// 默认为0
                "and[2][operator]": "EQ",
                "and[2][fieldName]": "dbname",					    // 早教中心数据源
                "and[2][fieldValue]": $(obj).val()					// dbname
            },
            async : false,
            error : function(data) {
                alert("error" + data.status);
            },
            success : function(data) {
                console.info(data);

                if(data.success == true){
                    var typeTitle = "请选择分支机构";

                    var inner='<option value="all">'+typeTitle+'</option>';
                    var dataList = data.data.rows;
                    if (data != null) {
                        for(var i=0;i<dataList.length;i++){

                            inner+='<option value="'+dataList[i].id +'">'+dataList[i].name+'</option>';
                        }
                    }
                    $("#shopSelect").html(inner);
                }
            }
        });
    }

    // 发送jpush公告
    function sendJpushMessage() {
        var platform = "all";   // 推送平台,默认为所有
        var tag = "all";        // 推送机构，默认为所有
// 		var tag = [];

		if($("#msgTitle").val().trim().length == 0){
            alert("通知标题不能为空！");
            $("#msgTitle").focus();
            return;
        }
		
        if($("#msgContent").val().trim().length == 0){
            alert("发送内容不能为空！");
            $("#msgContent").focus();
            return;
        }

        // 都没选中时
        if(($("#id_aps").prop("checked") == false) && ($("#id_android").prop("checked") == false)) {
            alert("请至少选择一个推送平台！");
            return;
        }

        // 只选中ios时
        if(($("#id_aps").prop("checked") == true) && ($("#id_android").prop("checked") == false)) {
            platform = "ios";
        }

        // 选中androids时
        if(($("#id_aps").prop("checked") == false) && ($("#id_android").prop("checked") == true)) {
            platform = "android";
        }

        // 都选中时
        if(($("#id_aps").prop("checked") == true) && ($("#id_android").prop("checked") == true)) {
            platform = "all";
        }
        
        if($("#centerSelect").val() == "all"){
        	// 推送所有早教中心
//         	tag.push("all");
        	tag = "all";
        } else {
        	if($("#shopSelect").val() == "all"){
        		// 一个早教中心下所有分支机构 
        		/* for(var i = 0; i < $("#centerSelect").children().length; i++){
        			alert($(i).val());
        		} */
        		alert("请选择选要发送的分支机构！"); 
        		$("#shopSelect").focus();
        		return;
        	} else {
        		// 指定的单个机构 
        		tag = $("#shopSelect").val();
        	}
        } 
        


        if (confirm("确认现在发送？")) {
            $.ajax({
                url: "/miwawa/app/push/message/",
                type: "POST",
                dataType: "json",
                data: {
                    "type": "text",
                    "title": $("#msgTitle").val(),
                    "content": $("#msgContent").val(),
                    "platform": platform,
//                    "tag": "4028824853d1b4c60153d1b9c5a70003"
                    "tag": tag // "tag1"
                },
                async: false,
                error: function (data) {
//                    alert("error" + data.status);
                },
                success: function (data) {
//                     alert(data);
                    console.info(data);
                }
            });
        }
    }
</script>
</body>
</html>